package com.kungfuhacking.wristbandpro.my.activity;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.presenter.PhotoPresenter;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.my.bean.OldDetail;
import com.kungfuhacking.wristbandpro.my.presenter.AttentionAdapter;
import com.kungfuhacking.wristbandpro.my.presenter.IOldDetailPresenter;
import com.kungfuhacking.wristbandpro.my.view.IOldDetailView;
import com.kungfuhacking.wristbandpro.utils.AbDateUtil;
import com.kungfuhacking.wristbandpro.widget.ActionSheetDialog;
import com.kungfuhacking.wristbandpro.widget.MyDatePicker;
import com.kungfuhacking.wristbandpro.widget.swipe.DividerItemDecoration;

import java.util.Calendar;
import java.util.Date;

/**
 * 老人详情页面
 * Created by Aaron on 17/5/25.
 */

public class OldDetailActivity extends BaseActivity implements IOldDetailView {

    private static final int TAKE_REQUEST_CODE = 0x323;
    private static final int CHOOSE_PHOTO_REQUEST_CODE = 0x324;
    private static final int MODIFY_OLD_NAME_REQUEST_CODE = 0x325;
    private static final int MODIFY_OLD_SEX_REQUEST_CODE = 0x326;
    private RecyclerView recycleView;
    private String id; //老人的id
    //    private String classInfo;
    private OldDetail oldDetail = new OldDetail();
    private IOldDetailPresenter iOldDetailPresenter = new IOldDetailPresenter(this);
    private AttentionAdapter attentionAdapter;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_old_details);
        id = getIntent().getStringExtra("id");
//        classInfo = getIntent().getStringExtra("classInfo");
        initWidget();
    }

    @Override
    protected void onResume() {
        super.onResume();
        iOldDetailPresenter.oldDetailInfo(id);
    }

    private void initWidget() {

        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.tbv);
        titleBarView.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recycleView = (RecyclerView) findViewById(R.id.recycleView);
        initRecycleView();

    }

    private void initRecycleView() {

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setItemAnimator(new DefaultItemAnimator());
        attentionAdapter = new AttentionAdapter(this, oldDetail);
        recycleView.setAdapter(attentionAdapter);
        recycleView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST,
                getResources().getDrawable(R.color.bg_gray)));

    }

    @Override
    public void setAdapterInfp(OldDetail oldDetail) {
        this.oldDetail = oldDetail;
        attentionAdapter.setOldDetail(oldDetail);
        attentionAdapter.notifyDataSetChanged();
    }

    @Override
    public void setUpdateInfo(String url, String birthday, String name, String sex) {
        oldDetail.getData().setBirthDay(birthday);
        oldDetail.getData().setPicUrl(url);
        oldDetail.getData().setChildName(name);
        oldDetail.getData().setSex(sex);
        attentionAdapter.setUpdatedInfo(url, birthday, name, sex);
    }

    @Override
    public String getBirthday() {
        return oldDetail.getData().getBirthDay();
    }

    @Override
    public void setName(String name) {

    }

    @Override
    public void setSex(String sex) {
    }

    @Override
    public void finishPage() {
        finish();
    }

    @Override
    public Context getContext() {
        return this;
    }

//    @Override
//    public void setBirthday(String birthday) {
//
//    }

    /**
     * 权限请求, 升级 降级 删除
     * //     * @param id     孩子的id
     *
     * @param level    被调整的等级
     * @param phoneNum 被调整人的手机号码
     */
    public void levelAdjust(int level, String phoneNum) {
        iOldDetailPresenter.modifyPermission(id, level, phoneNum);
    }

    /**
     * 修改头像
     */
    public void modifyImg() {

        if (iOldDetailPresenter.isCanModifyInfo())
            showChoose();
        else
            showErr("权限不够");
    }

    /**
     * 弹出选择照片的对话框
     */
    @Deprecated
    protected void showChoose() {

        new ActionSheetDialog(this)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("拍照", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 拍照
                                iOldDetailPresenter.getTakePhoto(TAKE_REQUEST_CODE, OldDetailActivity.this);

                            }

                        })
                .addSheetItem("去相册选择", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 从相册中选择
                                iOldDetailPresenter.getFromPhotoAlbum(CHOOSE_PHOTO_REQUEST_CODE, OldDetailActivity.this);
                            }
                        }).show();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {

            case TAKE_REQUEST_CODE://拍照返回的code
                if (iOldDetailPresenter.getCurrentFile() != null && iOldDetailPresenter.getCurrentFile().exists())
                    iOldDetailPresenter.startPhotoZoom(Uri.fromFile(iOldDetailPresenter.getCurrentFile()));//裁剪
                break;

            case CHOOSE_PHOTO_REQUEST_CODE://从相册选择返回的code
                if (data == null)
                    return;
                iOldDetailPresenter.startPhotoZoom(iOldDetailPresenter.checkSelectPhoto(data, this));//开始裁剪
                break;

            case PhotoPresenter.PHOTO_RESOULT://裁剪后返回的code
                if (data == null)
                    return;
                iOldDetailPresenter.getToken(data, id);
                break;

            case MODIFY_OLD_NAME_REQUEST_CODE://修改老人姓名
                if (data == null)
                    return;
                iOldDetailPresenter.updateOldInfo(id, oldDetail.getData().getPicUrl(),
                        oldDetail.getData().getBirthDay(), data.getStringExtra("name"), oldDetail.getData().getSex());
                break;

            case MODIFY_OLD_SEX_REQUEST_CODE://修改老人性别
                if (data == null)
                    return;
                iOldDetailPresenter.updateOldInfo(id, oldDetail.getData().getPicUrl(),
                        oldDetail.getData().getBirthDay(), oldDetail.getData().getChildName(), data.getStringExtra("sex"));
                break;

        }

    }

    /**
     * 修改姓名
     */
    public void modifyName() {

        if (iOldDetailPresenter.isCanModifyInfo()) {
            mIntent = new Intent(this, ModifyOldNameActivity.class);
            mIntent.putExtra("oldInfo", oldDetail);
            startActivityForResult(mIntent, MODIFY_OLD_NAME_REQUEST_CODE);
        } else
            showErr("权限不够");
    }

    /**
     * 修改性别
     */
    public void modifySex() {

        if (iOldDetailPresenter.isCanModifyInfo()) {
            mIntent = new Intent(this, ModifyOldSexActivity.class);
            mIntent.putExtra("oldInfo", oldDetail);
            startActivityForResult(mIntent, MODIFY_OLD_SEX_REQUEST_CODE);
        } else
            showErr("权限不够");
    }

    /**
     * 修改生日
     */
    public void modifyBirthday() {
        if (iOldDetailPresenter.isCanModifyInfo())
            moifyBirthday();
        else
            showErr("权限不够");
    }

    private void moifyBirthday() {
        //选择生日
//                String birthday = CommonUtils.isNull(tv_birthday.getText().toString().trim()) ? "" : tv_birthday.getText().toString().trim();
        Date date = null;

        if (!TextUtils.isEmpty(oldDetail.getData().getBirthDay()))
            date = AbDateUtil.getDateByFormat(oldDetail.getData().getBirthDay(), AbDateUtil.DATEFORMATYMD);

        Calendar c = Calendar.getInstance();
        if (date != null)
            c.setTime(date);

        new MyDatePicker(this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {

                String month = (monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";
                String day = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";

                iOldDetailPresenter.updateOldInfo(id, oldDetail.getData().getPicUrl(), year + "-" + month + "-" + day
                        , oldDetail.getData().getChildName(), oldDetail.getData().getSex());
            }

        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH)).show();
    }

//    /**
//     * 家庭信息
//     */
//    public void familyInfo() {
//        if (iOldDetailPresenter.isCanModifyInfo()) {
//            mIntent = new Intent(this, FamilyDetailsActivity.class);
//            mIntent.putExtra("childId", id);
//            mIntent.putExtra("level", getIntent().getIntExtra("level", 0));
//            startActivity(mIntent);
//        } else
//            showErr("权限不够");
//    }

    //删除老人
    public void delArea() {
        iOldDetailPresenter.deleteOld(id);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iOldDetailPresenter.clearRequest();
    }
}
